<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\ChapterDetail as UM;
use think\Db;

class ChapterDetail extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }

    /**
    * 章节列表
    */
    public function index()
    {
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        $count = $model->_count($where);
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        
        return $this->fetch();
    }

    /**
    * 添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            
            $ret = $model->_create($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{
            $request = $this->request->get();
            $this->assign([
                'chapter_cate' => (int)$request['chapter_cate']
            ]);

            return $this->fetch();
        }
    }
    
    /**
    * 编辑管理员
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            $ret = $model->_modify($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $request = $this->request->get();

            $id = (int)$request['id'];
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $this->assign([
                'data'=>$data,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除管理员
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->param('id');
//            $is_have = \app\admin\model\Single::where('detail_id',$id)->find();
//            if($is_have){
//                die(outputError('请先删除模拟真题下的试题'));
//            }
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }

    /**
     * 章节列表
     */
    public function index_one()
    {
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where_one($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list_one($where);
        $count = $model->_count($where);

        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);

        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add_one(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();

            $ret = $model->_create_one($request);
            if(!$ret){
                die(outputError($model->getError()));
            }

            die(outputSuccess([], '添加成功'));
        }else{
            $cates = \app\admin\model\Question::all()->toArray();
            $this->assign([
                'cates' => $cates,
            ]);
            return $this->fetch();
        }
    }

    /**
     * 编辑管理员
     */
    public function edit_one(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            $ret = $model->_modify($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $request = $this->request->get();

            $id = (int)$request['id'];
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $cates = \app\admin\model\Question::all()->toArray();
            $this->assign([
                'data'=>$data,
                'cates'=>$cates
            ]);
            return $this->fetch();
        }
    }

    /**
     * 删除管理员
     */
    public function del_one(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            //查看是否有子章节
            $is_have = \app\admin\model\Single::where('detail_id',$id)->find();
            if($is_have){
                die(outputError('请先删除模拟真题下的试题'));
            }
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }

            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }

    /**
     * 批量删除
     */
    public function delAll_one(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }

            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }

    /**
     * 添加试题
     */
    public function add_questions(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $file    = $this->request->file('file');
            if($request['q_type'] ==1){
                $data_arr = $this->bath_one($file,$request);
            }elseif ($request['q_type'] ==2){
                $data_arr = $this->bath_two($file,$request);
            }elseif ($request['q_type'] ==3){
                $data_arr = $this->bath_three($file,$request);
            }
            $count    = count($data_arr);
            $Model    = new \app\admin\model\Single();
            $ret      = $Model->saveAll($data_arr);
            if(!$ret){
                die(outputError('上传失败'));
            }
            die(outputSuccess([], '上传成功，共添加'.$count.'道试题'));
        }else{
            $request = $this->request->get();
            $id = (int)$request['id'];
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $this->assign([
                'data'=>$data,
            ]);
            return $this->fetch();
        }
    }



    /**
     * 单选题上传处理
     */
    public function bath_one($file,$come)
    {
        $info = $file->validate(['size'=>1024*1024,'ext'=>'xlsx,xls,csv'])->move( './upload/excel');
        $exclePath = $info->getSaveName();  //获取文件名
        $file_name =   './upload/excel/'. $exclePath;   //上传文件的地址
        $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
        $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
        $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        array_shift($excel_array);  //删除第一个数组(标题);
        $data     = [];
        $data_arr = [];
        foreach($excel_array as $k=>$v) {
//            $data['q_num']   = $v[0];
//            $data['q_title'] = $v[1];
//            $data['q_a'] = $v[2];
//            $data['q_b'] = $v[3];
//            $data['q_c'] = $v[4];
//            $data['q_d'] = $v[5];
//            $data['q_site'] = $v[6];
//            $data['q_answer'] = $v[7];
//            $data['q_analysis'] = $v[8];
//            $data['is_free'] = empty($v[9])?0:$v[9];
//            $data['q_score'] = empty($v[10])?:$v[10];
//            $data['q_origin'] = empty($v[11])?null:$v[11];
//            $data['cate']          =  $come['cate'];
//            $data['q_type']        =  $come['q_type'];
//            $data['chapter_cate']  =  $come['chapter_cate'];
//            $data['detail_id']     =  $come['detail_id'];
//            $data['create_at']     =  time();
//            $data_arr[] = $data;
            $data['q_num']   = $v[0];
            $data['q_title'] = $v[1];
            $data['q_a'] = $v[2];
            $data['q_b'] = $v[3];
            $data['q_c'] = $v[4];
            $data['q_d'] = $v[5];
            $data['q_e'] = $v[6];
            $data['q_site'] = $v[7];
            $data['q_answer'] = $v[8];
            $data['q_analysis'] = $v[9];
            $data['is_free'] = empty($v[10])?0:$v[10];
            $data['q_score'] = empty($v[11])?0:$v[11];
            $data['q_origin'] = empty($v[12])?null:$v[12];
            $data['cate']          =  $come['cate'];
            $data['q_type']        =  $come['q_type'];
            $data['chapter_cate']  =  $come['chapter_cate'];
            $data['detail_id']     =  $come['detail_id'];
            $data['create_at']     =  time();
            $data_arr[] = $data;
        }
        return $data_arr;
    }

    /**
     * 多选题上传处理
     */
    public function bath_two($file,$come)
    {
        $info = $file->validate(['size'=>1024*1024,'ext'=>'xlsx,xls,csv'])->move( './upload/excel');
        $exclePath = $info->getSaveName();  //获取文件名
        $file_name =   './upload/excel/'. $exclePath;   //上传文件的地址
        $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
        $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
        $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        array_shift($excel_array);  //删除第一个数组(标题);
        $data     = [];
        $data_arr = [];
        foreach($excel_array as $k=>$v) {
            $data['q_num']   = $v[0];
            $data['q_title'] = $v[1];
            $data['q_a'] = $v[2];
            $data['q_b'] = $v[3];
            $data['q_c'] = $v[4];
            $data['q_d'] = $v[5];
            $data['q_e'] = $v[6];
            $data['q_site'] = $v[7];
            $data['q_answer'] = $v[8];
            $data['q_analysis'] = $v[9];
            $data['is_free'] = empty($v[10])?0:$v[10];
            $data['q_score'] = empty($v[11])?0:$v[11];
            $data['q_origin'] = empty($v[12])?null:$v[12];
            $data['cate']          =  $come['cate'];
            $data['q_type']        =  $come['q_type'];
            $data['chapter_cate']  =  $come['chapter_cate'];
            $data['detail_id']     =  $come['detail_id'];
            $data['create_at']     =  time();
            $data_arr[] = $data;
        }
        return $data_arr;
    }

    /**
     * 简答题上传处理
     */
    public function bath_three($file,$come)
    {
        $info = $file->validate(['size'=>1024*1024,'ext'=>'xlsx,xls,csv'])->move( './upload/excel');
        $exclePath = $info->getSaveName();  //获取文件名
        $file_name =   './upload/excel/'. $exclePath;   //上传文件的地址
        $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
        $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
        $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        array_shift($excel_array);  //删除第一个数组(标题);
        $data     = [];
        $data_arr = [];
        foreach($excel_array as $k=>$v) {
            if($v[0]) {
                $data['q_num'] = $v[0];
                $data['q_title'] = $v[1];
                $data['q_site'] = $v[2];
                $data['q_answer'] = $v[3];
                $data['q_analysis'] = $v[4];
                $data['is_free'] = empty($v[5]) ? 0 : $v[5];
                $data['q_score'] = empty($v[6]) ? 0 : $v[6];
                $data['q_origin'] = empty($v[7]) ? null : $v[7];
                $data['cate'] = $come['cate'];
                $data['q_type'] = $come['q_type'];
                $data['chapter_cate'] = $come['chapter_cate'];
                $data['detail_id'] = $come['detail_id'];
                $data['create_at'] = time();
                $data_arr[] = $data;
            }
        }
        return $data_arr;
    }

    /**
     * 章节试题列表
     */
    public function index_two()
    {
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where_two($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list_two($where);
        $count = $model->_count($where);
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        return $this->fetch();
    }



}
